﻿-- Author      : Kurapica
-- Create Date : 6/12/2008 1:14:03 AM

-- Frame
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.Frame", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "Frame"
    local _Base = "Region"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
		
	-- ScriptType
	local _ScriptType = {
	}
    
	-- FuncProxy
	local _FuncProxy = {
		-- Dispose, release resource
        ["Dispose"] = function(self)
            self:UnregisterAllEvents()
            
            -- Call super's dispose
			self:CallMethod(_Widget[_Base], "Dispose")
        end,
        
        -- RegisterEvent
        ["RegisterEvent"] = function(self, event)
            self.__UI:RegisterEvent(event)
        end,
        
        -- UnregisterAllEvents
        ["UnregisterAllEvents"] = function(self)        
            self.__UI:UnregisterAllEvents()
        end,
        
        -- UnregisterEvent
        ["UnregisterEvent"] = function(self, event)
            self.__UI:UnregisterEvent(event)
        end,
		
		-- StackScript
		["StackScript"] = function(self, handler, func, flag)
			if type(handler) ~= "string" then
				error("The handler's name must be a string", 2)
			end
			if func and type(func) ~= "function" then
				error("The handler must be a function", 2)
			end
			_BaseFunc.StackScript(self, handler, func, flag)
		end,
		
		-- SetBackdrop
		["SetBackdrop"] = function(self, backdropTable)
			self.__UI:SetBackdrop(backdropTable or nil)
		end,
	}
	
	-- Property
	local _Property = {	
		-- Frame		
		["KeyboardEnabled"] = {
			["Set"] = function(self, enabled)
				if not self.InDesignMode then
					self.__UI:EnableKeyboard(enabled)
				else
					self.__KeyboardEnabled = (enabled and true) or false
				end
			end,
			
			["Get"] = function(self)
				if not self.InDesignMode then 
					return (self.__UI:IsKeyboardEnabled() and true) or false
				else
					return (self.__KeyboardEnabled and true) or false
				end
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		
		["MouseEnabled"] = {
			["Set"] = function(self, enabled)
				if not self.InDesignMode then
					self.__UI:EnableMouse(enabled)
				else
					self.__MouseEnabled = (enabled and true) or false
				end
			end,
			
			["Get"] = function(self)
				if not self.InDesignMode then
					return (self.__UI:IsMouseEnabled() and true) or false
				else
					return (self.__MouseEnabled and true) or false
				end
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		
		["MouseWheelEnabled"] = {
			["Set"] = function(self, enabled)
				self.__UI:EnableMouseWheel(enabled)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsMouseWheelEnabled() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		                  
		["Backdrop"] = {
			["Set"] = function(self, backdropTable)
				self:SetBackdrop(backdropTable)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetBackdrop()
			end,
			
			["Type"] = _PropertyType.BackdropTable,
		},
		      
		["BackdropBorderColor"] = {
			["Set"] = function(self, colorTable)
				self.__UI:SetBackdropBorderColor(colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha)
			end,
			
			["Get"] = function(self)
				local colorTable = {}
				colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha = self.__UI:GetBackdropBorderColor()
				return colorTable 
			end,
			
			["Type"] = _PropertyType.Color,
		},
		
		["BackdropColor"] = {
			["Set"] = function(self, colorTable)
				self.__UI:SetBackdropColor(colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha)
			end,
			
			["Get"] = function(self)
				local colorTable = {}
				colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha = self.__UI:GetBackdropColor()
				return colorTable 
			end,
			
			["Type"] = _PropertyType.Color,
		},
		    
		["ClampedToScreen"] = {
			["Set"] = function(self, enabled)
				self.__UI:SetClampedToScreen(enabled)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsClampedToScreen() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		     
		["ClampRectInsets"] = {
			["Set"] = function(self, RectInset)
				self.__UI:SetClampRectInsets(RectInset.left, RectInset.right, RectInset.top, RectInset.bottom)
			end,
			
			["Get"] = function(self)
				local RectInset = {}
				RectInset.left, RectInset.right, RectInset.top, RectInset.bottom = self.__UI:GetClampRectInsets()
				return RectInset 
			end,
			
			["Type"] = _PropertyType.RectInset,
		},
		                
		["FrameLevel"] = {
			["Set"] = function(self, level)
				self.__UI:SetFrameLevel(level)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetFrameLevel()
			end,
			
			["Type"] = _PropertyType.Number,
		},
		
		["FrameStrata"] = {
			["Set"] = function(self, strata)
				self.__UI:SetFrameStrata(strata) 
			end,
			
			["Get"] = function(self)
				return self.__UI:GetFrameStrata()
			end,
			
			["Type"] = _PropertyType.FrameStrata,
		},
		
		["HitRectInsets"] = {
			["Set"] = function(self, RectInset)
				self.__UI:SetHitRectInsets(RectInset.left, RectInset.right, RectInset.top, RectInset.bottom)
			end,
			
			["Get"] = function(self)
				local RectInset = {}
				RectInset.left, RectInset.right, RectInset.top, RectInset.bottom = self.__UI:GetHitRectInsets()
				return RectInset 
			end,
			
			["Type"] = _PropertyType.RectInset,
		},
		                
		["ID"] = {
			["Set"] = function(self, id)
				self.__UI:SetID(id) 
			end,
			
			["Get"] = function(self)
				return self.__UI:GetID()
			end,
			
			["Type"] = _PropertyType.Number,
		},
		
		["MaxResize"] = {
			["Set"] = function(self, Size)
				self.__UI:SetMaxResize(Size.width, Size.height)
			end,
			
			["Get"] = function(self)
				local Size = {}
				Size.width, Size.height = self.__UI:GetMaxResize()
				return Size 
			end,
			
			["Type"] = _PropertyType.Size,
		},
		                
		["MinResize"] = {
			["Set"] = function(self, Size)
				self.__UI:SetMinResize(Size.width, Size.height)
			end,
			
			["Get"] = function(self)
				local Size = {}
				Size.width, Size.height = self.__UI:GetMinResize()
				return Size 
			end,
			
			["Type"] = _PropertyType.Size,
		},
		                   
		["Scale"] = {
			["Set"] = function(self, scale)
				self.__UI:SetScale(scale) 
			end,
			
			["Get"] = function(self)
				return self.__UI:GetScale()
			end,
			
			["Type"] = _PropertyType.Number,
		},
		        
		["Movable"] = {
			["Set"] = function(self, enabled)
				if not self.InDesignMode then
					self.__UI:SetMovable(enabled)
				else
					self.__Movable = (enabled and true) or false
				end
			end,
			
			["Get"] = function(self)
				if not self.InDesignMode then
					return (self.__UI:IsMovable() and true) or false
				else
					return (self.__Movable and true) or false
				end
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		          
		["Resizable"] = {
			["Set"] = function(self, enabled)
				if not self.InDesignMode then
					self.__UI:SetResizable(enabled)
				else
					self.__Resizable = (enabled and true) or false
				end
			end,
			
			["Get"] = function(self)
				if not self.InDesignMode then
					return (self.__UI:IsResizable() and true) or false
				else
					return (self.__Resizable and true) or false
				end
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		          
		["Toplevel"] = {
			["Set"] = function(self, enabled)
				self.__UI:SetToplevel(enabled)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsToplevel() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		          
		["UserPlaced"] = {
			["Set"] = function(self, enabled)
				self.__UI:SetUserPlaced(enabled)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsUserPlaced() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},		          
	}
	    
    -- New      
    local _New = function(_parent,inheritFrame)
        -- New Frame
		local frame = CreateFrame(_WidgetName,nil,_BaseFunc.GetUI(_parent),inheritFrame)
		
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
    
	------------------------------------------------------
	--------------- Scan new function --------------------
    ------------------------------------------------------
	local _frame = CreateFrame(_WidgetName,nil,UIParent)
	
	local _oriMeta = getmetatable(_frame)
	_oriMeta = _oriMeta.__index
    
    local _func = _Widget[_WidgetName]["FuncProxy"]
	-- Other Function
	for _f in pairs(_oriMeta) do
		if not _func[_f] and type(_oriMeta[_f]) == "function" then
			_func[_f] = function(self, ...)
                return _BaseFunc.GetVals(self.__UI[_f](self.__UI, _BaseFunc.GetArgs(...)))
			end
		end
	end
	
	-- Clear
	_oriMeta = nil
    _frame:Hide()
	_frame = nil
end